﻿/*

========================================================================

	                        Light Prepass Renderer

		                        by Floris Groen

                         E-mail : fmfgroen@yahoo.com
                     Blog : www.florisgroen.blogspot.com
		      

========================================================================

*/

using LightPrePassRenderer.Engine.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LightPrePassRenderer.Engine
{
    public class DepthAndNormalsMapping : EffectParameterMapping
    {
        EffectParameter farPlane;
        EffectParameter world;
        EffectParameter view;
        EffectParameter projection;
        EffectParameter normals;
        EffectParameter specularPower;

        IRenderManager renderManager;

        public DepthAndNormalsMapping(Effect effect, GameServiceContainer services) : base(effect, services)
        {
            farPlane = effect.Parameters["FarPlane"];
            world = effect.Parameters["World"];
            view = effect.Parameters["View"];
            projection = effect.Parameters["Projection"];
            normals = effect.Parameters["Normal"];
            specularPower = effect.Parameters["SpecularPower"];

            renderManager = (IRenderManager)services.GetService(typeof(IRenderManager));
        }

        public override void SetParametersPerFrame()
        {
            Matrix viewMatrix, projectionMatrix;

            renderManager.Camera.GetView(out viewMatrix);
            renderManager.Camera.GetProjection(out projectionMatrix);

            farPlane.SetValue(renderManager.Camera.FarPlane);
            view.SetValue(viewMatrix);
            projection.SetValue(projectionMatrix);
        }

        public override void SetParametersPerObject(RenderEntry renderEntry)
        {
            world.SetValue(renderEntry.World);
            normals.SetValue(renderEntry.Material.Normals);
            specularPower.SetValue(renderEntry.Material.SpecularPower);

            Effect.CommitChanges();            
        }
    }
}
